<template>
	<scroll-view  :scroll-top="scrollTop" 
		scroll-y="true" 
		class="content-ques" 
		:lower-threshold='50'
		@scrolltolower="loadLower"
		@scroll="scroll">
		<view class="content-quesx" id="commentbox">
			<!--评论列表-->
			<five-mul-commentlist
				:commentList="commentList"
				:csize="csize"
				@clickPraise="clickPraiseComment"
				@clickDelete="clickDeleteComment"
				@clickDeleteChild="clickDeleteCommentChild"
				@clickRecomment="clickRecomment"
				@clickLoadMore="loadChildrenComment"
				@clickRecommentChild="clickRecommentChild"
			></five-mul-commentlist>
			<!--写评论-->
			<view class="view-conmment-send-bottom">
				<view class=" view-comment-textarea" @click="clickComment()"><view class="textarea-comment">写评论…</view></view>
			</view>
			
			<pug-loading v-if="total > 0" :bottom="40" :loadType="loadType" :loadingText="['数据正在赶来的路上','亲，数据加载成功了','亲，我也是有底线的']"></pug-loading>
			
			<!--发评论-->
			<five-comment ref="detailComment" @sendComment="sendComment" :placeholder="placeholder"></five-comment>
		</view>
	</scroll-view>
</template>

<script>
/**
 * @author: 小五
 * @time：	2020-07-07
 * 注：
 * 1. 评论详情 、回复评论及回复子评论功能实际是调用接口实现，在这里只为实现效果，逻辑无参考价值
 */
import data from '@/data';
import dateUtils from '@/common/utils/dateUtils.js';
import hotelCommentService from '@/service/hotel/HotelCommentService';

export default {
	props:{
		hotelid:{
			type:String,
			default:""
		}
	},
	
	data() {
		return {
			scrollTop: 0,
			
			csize:3, // 控制子评论现实的条数
			parentId:0,//父id
			parentIndex:-1,//记录当前操作的索引
			childrenIndex:-1,//记录当前操作的回复的索引
			
			loadType:-1,
			pageNo:1, //一级评论的起始页
			pageSize:15,//一级评论的每页展示的条目数
			pages:1,//一级评论的总页数
			total:0,//一级评论的总记录数
			commentList: [],//一级评论的数据集合容器
			
			//评论组件相关
			placeholder: '请输入评论内容…',
			commentParam: {}
		};
	},
	
	mounted(){
		// 加载评论
		// #ifdef H5
		this.getComment();
		// #endif
	},
	
	methods: {
		
		// 只要滚动就触发
		scroll: function(e) {
			this.scrollTop = e.detail.scrollTop
		},
		
		loadLower(){
			uni.$u.debounce(this.lower,500);
		},
		
		// 滚动分页距离底部50px触发
		lower: function(e) {
			if(this.pageNo == this.pages){
				this.loadType = 2;
				return;
			}
			this.pageNo++;
			this.getComment();
		},
		
		
		/**
		 * 获取评论详情
		 */
		async getComment() {
			try{
				
				this.loadType = 0
				var params = {pageNo:this.pageNo,pageSize:this.pageSize,hotelId:this.hotelid,parentId:0};
				var res =await hotelCommentService.loadHotelComment(params);
				// 获取服务器返回的评论信息
				var commentRecords = res.data.records;
				this.pages = res.data.pages;
				this.total = res.data.total;
				this.loadType = 1;
				this.commentList = this.commentList.concat(commentRecords.map(comment=>this.transferComment(comment)));
			}catch(e){
			}
		},
		
		/**
		 * 加载子评论
		 */
		async loadChildrenComment(item,index){
			var pageNo = this.commentList[index].pageNo;
			var pages = this.commentList[index].pages;
			var parentId = this.commentList[index].id;
			var hotelId = this.hotelid;
			if(pages == pageNo){
				this.commentList[index].msg = "ok";
				return;
			}
			
			pageNo++;
			this.commentList[index].pageNo = pageNo;
			var params = {pageNo:pageNo,pageSize:this.csize,hotelId:hotelId,parentId:parentId};
			try{
				var res = await hotelCommentService.loadHotelChildrenComment(params);
				var commentRecords = res.data.records;
				if(commentRecords && commentRecords.length > 0){
					// 转换数据
					commentRecords = commentRecords.map(comment=>this.transferComment(comment))
					// 这里为啥要返回去，改变数据形态去渲染视频
					this.commentList[index].CHILD_ANWSER_LIST = this.commentList[index].CHILD_ANWSER_LIST.concat(commentRecords);
				}
			}catch(e){
				
			}
		},

		
		/**
		 * 删除第一级评论
		 */
		clickDeleteComment(item,index) {
			var that = this;
			uni.showModal({
				title: '提示',
				content: '确定要删除评论吗？',
				confirmColor: '#12B368',
				success: async function(res) {
					if(res.confirm){
						/* 调用接口删除 */
						try{
							var res = await hotelCommentService.delHotelComment(that.hotelid,item.id);
							that.commentList.splice(index,1);
							that.total--;
							that.$emit("addComment",-1);
						}catch(e){
							uni.$u.toast("删除失败")
						}
					}
				}
			});
		},
		
		/**
		 * 删除子评论
		 */
		clickDeleteCommentChild(item,index,cindex) {
			var that = this;
			uni.showModal({
				title: '提示',
				content: '确定要删除评论吗？',
				confirmColor: '#12B368',
				success: async function(res) {
					if(res.confirm){
						/* 调用接口删除 */
						try{
							var res = await hotelCommentService.delHotelComment(that.hotelid,item.id);
							that.commentList[index].CHILD_ANWSER_LIST.splice(cindex,1);
							that.commentList[index].total--;
							that.$emit("addComment",-1);
						}catch(e){
							uni.$u.toast("删除失败")
						}
					}
				}
			});
		},

		
		/**
		 * 回复评论
		 * @param {Object} item
		 */
		clickRecommentChild(item,index,cindex) {
			this.commentParam = {};
			this.$refs.detailComment.open();
			this.placeholder = '回复' + item.FIRSTNICKNAME + ':' + index+":"+cindex;
		},
		
		/**
		 * 回复 评论
		 * @param {Object} item
		 */
		clickRecomment(item,index) {
			this.commentParam = {};
			this.$refs.detailComment.open();
			this.placeholder = '回复' + item.FIRSTNICKNAME + ':' + index;
			// 回复的评论是谁。
			this.parentId = item.id;
			this.parentIndex = index;
		},
		
		
		/**
		 * 发送评论
		 * @param {Object} result
		 */
		async sendComment(result) {
			// 获取评论的酒店id
			var hotelId = this.hotelid;
			// 评论的信息
			var params = {
				parentId:this.parentId,
				content:result,
				hotelId:hotelId
			};
			
			try{
				// 异步请求保存评论
				var res = await hotelCommentService.saveHotelComment(params)
				// 列表附加
				var commentInfo = this.transferComment(res.data);
				if(this.parentId>0){
					this.$emit("addComment",1);
					this.commentList[this.parentIndex].CHILD_ANWSER_LIST.push(commentInfo);
				}else{
					 this.commentList.unshift(commentInfo);
					 this.scrollTop = 0;
					 this.$emit("addComment",1);
					 
					//this.commentList.push(commentInfo);
					// var that = this;
					// uni.getSystemInfo({success(res) {
					// 	var height = res.windowHeight * 0.65;
					// 	const query = uni.createSelectorQuery().in(that);
					// 	query.select('#commentbox').boundingClientRect(data => {
					// 	  that.scrollTop = (data.height -height) + 80;
					// 	  console.log("得到的高度是：",(data.height -height));
					// 	  console.log("得到布局位置信息" + JSON.stringify(data));
					// 	  console.log("节点离页面顶部的距离为" + data.top);
					// 	}).exec();
					// }});
				}
				//重置原来状态
				this.parentId = 0;
				this.parentIndex = -1;
				this.placeholder = "请输入评论内容...";
			}catch(e){
				uni.$u.toast(e.message);
			}
		},
		
		/**
		 * 点赞多级评论
		 * @param {Object} item
		 */
		async clickPraiseComment(item,index) {
			try{
				if(item.IS_PRAISE == '1'){
					var res = await hotelCommentService.unLikeComment(item.id);
					// 取消点赞
					this.commentList[index].IS_PRAISE = '0';
					this.commentList[index].PRAISE_NUM--;
				}else{
					//点赞
					var res = await hotelCommentService.likeComment(item.id);
					this.commentList[index].IS_PRAISE = '1';
					this.commentList[index].PRAISE_NUM++;
				}
			}catch(e){
				uni.$uni.toast(e.message);
			}
		},
		
		
		transferComment(commentdb){
			 var result= {
				CANDELETE: 1 // 删除状态1未删除 0未删除
				,CHILD_ANWSER_LIST: Array(0) // 二级评论
				,COMMENT: commentdb.content //评论的内容
				,COMMENT_TIME: commentdb.createTime //评论的时间
				,FIRSTNICKNAME: commentdb.usernickname//评论人
				,HEADIMGURL: commentdb.useravatar//评论人的头像
				,IS_PRAISE: commentdb.iszan||0 // 1 已点赞 0为点赞
				,PRAISE_NUM: commentdb.zannum||0  // 点赞数量
				,SECONDNICKNAME: null
				,pageNo:1
				,pageNo:3
				,total:0
				,pages:1
				,msg:""
				,id: commentdb.id
				,parentId: commentdb.parentId
			}
			
			
			if(commentdb.pageChildrens && commentdb.pageChildrens.records.length > 0){
				var childrenList = commentdb.pageChildrens.records;
				childrenList = childrenList.map(comment=>this.transferComment(comment));
				result.pages = commentdb.pageChildrens.pages;
				result.total = commentdb.pageChildrens.total;
				result.pageNo = commentdb.pageChildrens.current;
				result.pageSize = commentdb.pageChildrens.size;
				// 添加子集评论
				result.CHILD_ANWSER_LIST = childrenList;
			}
			
			return result;
		},
		
		
		/**
		 * 删除单级评论
		 */
		clickDeleteSig(item) {
			uni.showModal({
				title: '提示',
				content: '确定要删除评论吗？',
				confirmColor: '#12B368',
				success: function(res) {
					/* 调用接口删除 */
				}
			});
		},
		
		/**
		 * 点赞单级评论
		 */
		clickPraiseSig(item) {
			//只能点赞不能取消
			if (item.IS_PRAISE == '1') {
				return;
			}
			item.IS_PRAISE = '1';
			item.PRAISE_NUM++;
		},
		

		
		/**
		 * 格式化发布日期
		 * @param {Object} dateString
		 */
		formateDate(dateString) {
			dateString = parseInt(dateString) * 1000;
			let date = new Date(dateString);
			let formatStr = dateUtils.dateFormat('YYYY年mm月dd日 HH:MM:SS', date);
			return formatStr;
		},
		
		/**
		 * 回复问题
		 */
		clickComment() {
			//重置原来状态
			this.parentId = 0;
			this.parentIndex = -1;
			this.childrenIndex = -1;
			this.commentParam = {};
			this.placeholder = "请输入评论内容...";
			//打开写评论的输入框
			this.$refs.detailComment.open();
		},
		
		/**
		 * 详情点赞
		 * @param {Object} isPraise
		 */
		clickPraise(isPraise) {
			let praiseParam = '1';
			let praiseNum = parseInt(this.detailInfo.PRAISE_NUM);
			switch (isPraise) {
				case '0':
					praiseParam = '1';
					praiseNum++;
					break;
				case '1':
					praiseParam = '0';
					praiseNum--;
					break;
			}
			this.detailInfo.IS_PRAISE = praiseParam;
			this.detailInfo.PRAISE_NUM = praiseNum--;
		},
		
		/**
		 *  查看图片大图
		 * @param {Object} images
		 * @param {Object} index
		 */
		cliclImage(images, index) {
			uni.previewImage({
				urls: images,
				current: index,
				longPressActions: {
					itemColor: '#12B368',
					success: function(data) {},
					fail: function(err) {}
				}
			});
		},
		
		
	}
};
</script>

<style lang="scss">
@import '../../common/css/iconfont.css';

.content-ques {
	background-color: $view-bg-color;
	position:fixed;
	max-height:70vh;/*viewport height*/
	bottom:0;
	right:0;
	left:0;
	border-top-right-radius: 10px;
	border-top-left-radius: 10px;
	z-index: 10076;
}

.view-delete-detail {
	flex: 1;
	display: flex;
	font-size: 26rpx;
}
.bottom-item {
	flex: 1;
	display: flex;
	flex-direction: row;
	align-items: center;
	height: 80rpx;
	.bottom-item-image {
		font-size: 40rpx;
		color: $uni-text-color;
	}
	.bottom-item-text {
		font-size: $uni-font-size-sm;
		margin-left: 20rpx;
		color: $uni-text-color;
	}
}
.view-content-content {
	background-color: white;
	flex-direction: column;
	padding: 30rpx 30rpx 0 30rpx;
}
.view-header {
	display: flex;
	align-items: center;
	flex-direction: row;

	.image-photo {
		height: 90rpx;
		width: 90rpx;
		background-color: $view-bg-color;
		border: 1px solid #eee;
		border-radius: 45rpx;
		margin-right: 30rpx;
	}

	.header-txt {
		display: flex;
		flex-direction: column;
		color: $uni-text-color-grey;
		line-height: 1.5;
		justify-content: center;
		font-size: $uni-font-size-sm;
		.name {
			color: #004261;
			font-weight: bold;
			font-size: $uni-font-size-base;
		}
	}
}
.sub {
	color: $uni-text-color-grey;
	font-size: $uni-font-size-sm;
}
.item-desc {
	color: $uni-text-color;
	line-height: 1.6;
	font-size: $uni-font-size-base;
	margin-top: 20rpx;
}

.item-imgs {
	margin: 20rpx 0;
	display: flex;
	flex-direction: row;
	-webkit-flex-wrap: wrap;
	flex-wrap: wrap;

	.image-imgs {
		border-radius: 10rpx;
		background-color: $view-bg-color;
		height: 220rpx;
		width: 220rpx;
		margin: 1px 4rpx;
	}
}

.view-conmment-send-bottom {
	width: 100%;
	position: fixed;
	bottom: 0;
	display: flex;
	flex-direction: row;
	background-color: white;
	border-top: 1px solid $view-bg-color;
}

.view-comment-textarea {
	background-color: $uni-bg-color-grey;
	border-radius: 40rpx;
	padding: 15rpx 20rpx;
	margin: 25rpx 20rpx;
	width: 80%;
	flex: 1;
	align-items: center;
}
.textarea-comment {
	padding: 0;
	// width: 100%;
	height: 100%;
	font-size: $uni-font-size-base;
	// color: $uni-text-color;
	color: $uni-text-sub-color;
}
.icon-comment {
	font-size: 46rpx;
	color: $uni-text-color;
	margin: 0 30rpx 0 5rpx;
}
.view-btn-comment {
	display: flex;
	justify-content: center;
	align-items: center;
	font-size: $uni-font-size-base;
	color: white;
	height: 70rpx;
	width: 120rpx;
	border-radius: 10rpx;
	background-color: $theme-color;
	font-weight: 500;
}
.textarea-bottom {
	background-color: white;
	padding: 20rpx;
}
</style>
